var page;//完工列表的页码
var _index = -1;//完工表的下标（用于表勾选）
var rowIndex = "%^%%%%";//完工表的下标（用于改变选中行样式）
var checkDatas = [];//获取质检项列表里的数据
var flag = false;//区分状态
var type = $("#type").text();//获取打开的页面是车间的还是办公室的，0为办公室，1为车间
var quality = false;//区分点击质检确定按钮，false没有，true有
var state=1;//质检和外协质检区别
var fNumberData,fOperSNData,tableHeight;
var url = $(".backIcon").context.URL;//判断是否是报工台页面的url
var flagMore = true;

// 当表格内容刷新时，保存当前完工列表的页码
var currentPageNumber;

/*
 * @Description: 预加载
 * @param : 
 */
$(function() {
	if(url.includes('QC_CJ')){
		tableHeight = $(window).height()*0.8;
	}else{
		tableHeight = $(window).height()*0.88;
	}
	resizeIframeHight();
	initCheckTable();
	baoname();
	if(type == 1){
		$("#condition").focus();
	}
	$.ajax({
		url : pathUrl("workCenter/getworkCenterIdAndName.action"),
		type : "GET",
		dataType : "JSON",
		async : false,
		success : function(data) {
  		var str='<option value="">请选择</option>';
  		for (var i=0;i<data.length;i++){
  			str=str+'<option value="'+data[i].text+'">'+data[i].value+'</option>';
  		}
  		$("#fWorkShopId").html(str);
  		$('#fWorkShopId').selectpicker('refresh');
		}
	});
	
	//点击Tab刷新表格
	$(".QC",parent.document).click(function(){
		$('#checkTable').bootstrapTable('refresh');
	});
});
/*
 * @Description: 质检按钮点击事件
 * @param : 
 */
$("#quality").click(function(){
	currentPageNumber = $('#checkTable').bootstrapTable('getOptions').pageNumber;
	$("#quality").addClass('changeColor');
	$("#qualityWX").removeClass('changeColor');
	state=1;
	initCheckTableAtCurrentPage(currentPageNumber);
	$("#workpalce").css("display","inline-block");
	$("#fCode").prev().css("display","inline-block");
	$("#fCode").css("display","inline-block");
});
/*
 * @Description: 外协质检按钮点击事件
 * @param : 
 */
$("#qualityWX").click(function(){
	currentPageNumber = $('#checkTable').bootstrapTable('getOptions').pageNumber;
	$("#qualityWX").addClass('changeColor');
	$("#quality").removeClass('changeColor');
	$("#workpalce").css("display","none");
	$("#fCode").prev().css("display","none");
	$("#fCode").css("display","none");
	state=2;
	initCheckTableAtCurrentPage(currentPageNumber);  
});
/*
 * @Description: 初始化质检记录表 
 * @param :  
 */
function initCheckTable() {
	loadingCustom('正在加载中。。。');
	var columns=[];
	var url="",show;
	if(type == 1){
		show = false;
	}else{
		show = true;
	}
	if(state==1){
		columns=[{
 			field : "fBillNo",
 			align : 'left',
 			valign: 'middle',
 			title : '生产任务单',
 			sortable : true,
 			edit : false
	 	},	{
 			field : "fCode",
 			align : 'left',
 			valign: 'middle',
 			title : '派工单号',
 			sortable : true,
 			edit : false
 		}, {
 			field : "fNumber",
 			align : 'left',
 			valign: 'middle',
 			title : '产品代码',
 			visible : show,
 			sortable : true,
 			edit : false
 		}, {
 			field : "fName",
 			align : 'left',
 			valign: 'middle',
 			title : '产品名称',
 			sortable : true,
 			edit : false
 		}, {
 			field : "fModel",
 			align : 'left',
 			valign: 'middle',
 			title : '规格型号',
 			sortable : true,
 			formatter : function(value,row,index){
				if(type == 1){
					return '<a href="javascript:void(0);" class="ggxh">'+value+'</a>';
				}else{
					return value;
				}
			},
			events : operateEvents,
 			edit : false
 		}, {
 			field : "fOperSN",
 			align : 'left',
 			valign: 'middle',
 			title : '工序号',
 			sortable : true,
 			edit : false
 		}, {
 			field : "fOper",
 			align : 'left',
 			valign: 'middle',
 			title : '工序名称',
 			sortable : true,
 			edit : false
 		},  {
 			field : "fQty",
 			align : 'left',
 			valign: 'middle',
 			title : '检验数',
 			edit : false,	
 			sortable : true,
 			formatter : function(value,row,index){
 				return row.fAuxOkQty == null?row.fQty:row.fAuxOkQty;
 			}
 		}, {
 			field : "fDate",
 			align : 'left',
 			valign: 'middle',
 			title : '完工时间',
 			edit : false,
 			sortable : true,
 			formatter:dateFormatter
 		},  {
 			field : "fWorkerName",
 			align : 'left',
 			valign: 'middle',
 			title : '操作工',
 			sortable : true,
 			edit : false
 		},{
 			field : 'operate',//如需导出execl field必填operate，否则操作行会被导出
 			align : 'left',
 			valign: 'middle',
 			title : '操作',
 			formatter : function(value,row,index){
 				if(type == 1){
 					return '<button type="button" class="btn btn-default pick" style="font-size:24px;margin-right:5px;">质检</button>';
 				}else{
 					return '<button type="button" class="btn btn-default pick" style="font-size:12px;margin-right:5px;">质检</button>';
 				}
 			},
 			events : operateEvents,
 			edit : false
 		}]
		url="end/findByCondition1.action";
	}else if(state==2){
		columns=[{
 			field : "fBillNo",
 			align : 'left',
 			valign: 'middle',
 			title : '生产任务单',
 			edit : false
 		}, {
 			field : "fNumber",
 			align : 'left',
 			valign: 'middle',
 			title : '产品代码',
 			visible : show,
 			edit : false
 		}, {
 			field : "fName",
 			align : 'left',
 			valign: 'middle',
 			title : '产品名称',
 			edit : false
 		}, {
 			field : "fModel",
 			align : 'left',
 			valign: 'middle',
 			title : '规格型号',
 			formatter : function(value,row,index){
				if(type == 1){
					return '<a href="javascript:void(0);" class="ggxh">'+value+'</a>';
				}else{
					return value;
				}
			},
			events : operateEvents,
 			edit : false
 		},  {
 			field : "fOperSN",
 			align : 'left',
 			valign: 'middle',
 			title : '工序号',
 			edit : false
 		}, {
 			field : "fOperName",
 			align : 'left',
 			valign: 'middle',
 			title : '工序名称',
 			edit : false
 		},{
 			field : "fReceiveQty",
 			align : 'left',
 			valign: 'middle',
 			title : '接收数',
 			edit : false
 		}, {
 			field : "fSupplierName",
 			align : 'left',
 			valign: 'middle',
 			title : '供应商',
 			edit : false
 		},   {
 			field : "fWorkerName",
 			align : 'left',
 			valign: 'middle',
 			title : '接收人',
 			edit : false
 		},{
 			field : 'operate',//如需导出execl field必填operate，否则操作行会被导出
 			align : 'left',
 			valign: 'middle',
 			title : '操作',
 			formatter : function(value,row,index){
 				if(type == 1){
 					return '<button type="button" class="btn btn-default pick" style="font-size:24px;margin-right:5px;">质检</button>';
 				}else{
 					return '<button type="button" class="btn btn-default pick" style="font-size:12px;margin-right:5px;">质检</button>';
 				}
 			},
 			events : operateEvents,
 			edit : false
 		}]
		url="subcin/getSubInQc.action";
	}
	$('#checkTable').bootstrapTable('destroy').bootstrapTable({
		url : pathUrl(url),//请求后台的URL
		height : tableHeight,//行高
		method : "POST",//请求方式
		//striped : true,//是否显示行间隔色
		cache : false,//是否使用缓存，默认为true
		pagination : true,//是否显示分页
		clickToSelect : true,//是否启用点击选中行
		singleSelect : true,//是否单选
		pageNumber : 1,//初始化加载第几页
		pageSize : 10,//每页的记录行数
		sidePagination : "server", // 分页方式：client客户端分页，server服务端分页（*）
		pageList : [ 10, 25, 50, 100 ],// 分页方式：client客户端分页，server服务端分页
		rowStyle : function(row, index) {//自定义样式，参数为：row：行数据，index：行下标
			var classes = [ 'success' ];
			if (index == rowIndex) {
				return {
					classes : classes[0]
				};
			}
			return {};
		},
		queryParams : function(params) {//传递参数
			var data= {}, condition;
			data.offset = params.offset;
			data.row = params.limit;
			var orderBy = params.sort + " " + params.order;      //排序列名
			if(params.sort==undefined){
				orderBy = "fDate desc";
			}
			//data.sortOrder = params.order;//排序
			//data.sortName = params.sort;//排序
			if(type == 0){
				condition = $("#condition").val();
				data.fMacSource = 0;
			}else{
				condition = $("#condition").val() == ""?-1:$("#condition").val();
				data.fMacSource = 1;
				data.fIsBgt = 1;
			}
			data.fBillNo=$("#fBillNo").val();
			data.fWorkShopId=$("#fWorkShopId").val();
			data.fCode=$("#fCode").val();
			data.fWorker=$("#fWorker").val();
			data.condition = condition;
			data.fChecker = $("#fChecker").val();
			data.mark=1;
			data.orderBy = orderBy;
			console.log(data);
			return data;
		},
		dataType : 'JSON',//数据类型
		uniqueId : "id",//每一行的唯一标识，一般为主键列
		columns : columns,
		onClickRow : function(row, $element) {//行单击
//			console.log(row)
//			rowId = row.id;
			$('.success').removeClass('success');// 去除之前选中的行的，选中样式
			$($element).addClass('success');// 添加当前选中的 success样式用于区别
			index = $element.data('index');
			rowIndex = index;
			page = $('.pagination .active').find("a").eq(0).text();
		},
		onPageChange : function(number, size) {//翻页
			if (page == number) {
				rowIndex = index;
			} else {
				$('.success').removeClass('success');
				rowIndex = "%^%%%%";
			}
		},
		onUncheck : function(index){//取消勾选
			$('.success').removeClass('success');
			rowIndex = "%^%%%%";
		},
		onLoadSuccess:function(data){
			swal.close();//关闭sweetAlert2提示框
			console.log(data);
		}
	});
}

/** 
 * @description: 刷新表格后，重新初始化质检记录表并停留在当前页 
 * @param :  
 */
function initCheckTableAtCurrentPage(currentPageNumber) {
	loadingCustom('正在加载中。。。');
	var columns=[];
	var url="",show;
	if(type == 1){
		show = false;
	}else{
		show = true;
	}
	if(state==1){
		columns=[{
 			field : "fBillNo",
 			align : 'left',
 			valign: 'middle',
 			title : '生产任务单',
 			edit : false
	 	},	{
 			field : "fCode",
 			align : 'left',
 			valign: 'middle',
 			title : '派工单号',
 			edit : false
 		}, {
 			field : "fNumber",
 			align : 'left',
 			valign: 'middle',
 			title : '产品代码',
 			visible : show,
 			edit : false
 		}, {
 			field : "fName",
 			align : 'left',
 			valign: 'middle',
 			title : '产品名称',
 			edit : false
 		}, {
 			field : "fModel",
 			align : 'left',
 			valign: 'middle',
 			title : '规格型号',
 			formatter : function(value,row,index){
				if(type == 1){
					return '<a href="javascript:void(0);" class="ggxh">'+value+'</a>';
				}else{
					return value;
				}
			},
			events : operateEvents,
 			edit : false
 		}, {
 			field : "fOperSN",
 			align : 'left',
 			valign: 'middle',
 			title : '工序号',
 			edit : false
 		}, {
 			field : "fOper",
 			align : 'left',
 			valign: 'middle',
 			title : '工序名称',
 			edit : false
 		},  {
 			field : "fQty",
 			align : 'left',
 			valign: 'middle',
 			title : '检验数',
 			edit : false,	
 			formatter : function(value,row,index){
 				return row.fAuxOkQty == null?row.fQty:row.fAuxOkQty;
 			}
 		}, {
 			field : "fDate",
 			align : 'left',
 			valign: 'middle',
 			title : '完工时间',
 			edit : false,
 			formatter:dateFormatter
 		},  {
 			field : "fWorkerName",
 			align : 'left',
 			valign: 'middle',
 			title : '操作工',
 			edit : false
 		},{
 			field : 'operate',//如需导出execl field必填operate，否则操作行会被导出
 			align : 'left',
 			valign: 'middle',
 			title : '操作',
 			formatter : function(value,row,index){
 				if(type == 1){
 					return '<button type="button" class="btn btn-default pick" style="font-size:24px;margin-right:5px;">质检</button>';
 				}else{
 					return '<button type="button" class="btn btn-default pick" style="font-size:12px;margin-right:5px;">质检</button>';
 				}
 			},
 			events : operateEvents,
 			edit : false
 		}]
		url="end/findByCondition1.action";
	}else if(state==2){
		columns=[{
 			field : "fBillNo",
 			align : 'left',
 			valign: 'middle',
 			title : '生产任务单',
 			edit : false
 		}, {
 			field : "fNumber",
 			align : 'left',
 			valign: 'middle',
 			title : '产品代码',
 			visible : show,
 			edit : false
 		}, {
 			field : "fName",
 			align : 'left',
 			valign: 'middle',
 			title : '产品名称',
 			edit : false
 		}, {
 			field : "fModel",
 			align : 'left',
 			valign: 'middle',
 			title : '规格型号',
 			formatter : function(value,row,index){
				if(type == 1){
					return '<a href="javascript:void(0);" class="ggxh">'+value+'</a>';
				}else{
					return value;
				}
			},
			events : operateEvents,
 			edit : false
 		},  {
 			field : "fOperSN",
 			align : 'left',
 			valign: 'middle',
 			title : '工序号',
 			edit : false
 		}, {
 			field : "fOperName",
 			align : 'left',
 			valign: 'middle',
 			title : '工序名称',
 			edit : false
 		},{
 			field : "fReceiveQty",
 			align : 'left',
 			valign: 'middle',
 			title : '接收数',
 			edit : false
 		}, {
 			field : "fSupplierName",
 			align : 'left',
 			valign: 'middle',
 			title : '供应商',
 			edit : false
 		},   {
 			field : "fWorkerName",
 			align : 'left',
 			valign: 'middle',
 			title : '接收人',
 			edit : false
 		},{
 			field : 'operate',//如需导出execl field必填operate，否则操作行会被导出
 			align : 'left',
 			valign: 'middle',
 			title : '操作',
 			formatter : function(value,row,index){
 				if(type == 1){
 					return '<button type="button" class="btn btn-default pick" style="font-size:24px;margin-right:5px;">质检</button>';
 				}else{
 					return '<button type="button" class="btn btn-default pick" style="font-size:12px;margin-right:5px;">质检</button>';
 				}
 			},
 			events : operateEvents,
 			edit : false
 		}]
		url="subcin/getSubInQc.action";
	}
	$('#checkTable').bootstrapTable('destroy').bootstrapTable({
		url : pathUrl(url),//请求后台的URL
		height : tableHeight,//行高
		method : "POST",//请求方式
		//striped : true,//是否显示行间隔色
		cache : false,//是否使用缓存，默认为true
		pagination : true,//是否显示分页
		clickToSelect : true,//是否启用点击选中行
		singleSelect : true,//是否单选
		pageNumber : currentPageNumber,//初始化加载第几页
		pageSize : 10,//每页的记录行数
		sidePagination : "server", // 分页方式：client客户端分页，server服务端分页（*）
		pageList : [ 10, 25, 50, 100 ],// 分页方式：client客户端分页，server服务端分页
		rowStyle : function(row, index) {//自定义样式，参数为：row：行数据，index：行下标
			var classes = [ 'success' ];
			if (index == rowIndex) {
				return {
					classes : classes[0]
				};
			}
			return {};
		},
		queryParams : function(params) {//传递参数
			var data= {}, condition;
			data.offset = params.offset;
			data.row = params.limit;
			//data.sortOrder = params.order;//排序
			//data.sortName = params.sort;//排序
			if(type == 0){
				condition = $("#condition").val();
				data.fMacSource = 0;
			}else{
				condition = $("#condition").val() == ""?-1:$("#condition").val();
				data.fMacSource = 1;
				data.fIsBgt = 1;
			}
			data.fBillNo=$("#fBillNo").val();
			data.fWorkShopId=$("#fWorkShopId").val();
			data.fCode=$("#fCode").val();
			data.fWorker=$("#fWorker").val();
			data.condition = condition;
			data.fChecker = $("#fChecker").val();
			data.mark=1;
			console.log(data);
			return data;
		},
		dataType : 'JSON',//数据类型
		uniqueId : "id",//每一行的唯一标识，一般为主键列
		columns : columns,
		onClickRow : function(row, $element) {//行单击
//			console.log(row)
//			rowId = row.id;
			$('.success').removeClass('success');// 去除之前选中的行的，选中样式
			$($element).addClass('success');// 添加当前选中的 success样式用于区别
			index = $element.data('index');
			rowIndex = index;
			page = $('.pagination .active').find("a").eq(0).text();
		},
		onPageChange : function(number, size) {//翻页
			if (page == number) {
				rowIndex = index;
			} else {
				$('.success').removeClass('success');
				rowIndex = "%^%%%%";
			}
		},
		onUncheck : function(index){//取消勾选
			$('.success').removeClass('success');
			rowIndex = "%^%%%%";
		},
		onLoadSuccess:function(data){
			swal.close();//关闭sweetAlert2提示框
			console.log(data);
		}
	});
}

/*
 * @Description: 表操作事件
 * @param :
 */
window.operateEvents= {
	'click .pick' : function(e, value, row, index) {//质检按钮点击事件
		$(".clean_c").val("");
		$("#fType").val(3);
		var ff=row.fScrapNotMeQty+row.fScrapQty;
		var yyy=$('#yyy').val(ff);
		console.log(yyy.val());
		$('#fType,#fStatus').selectpicker('refresh');
		$('#fQualityChkID,#fStatu').selectpicker('refresh');
//		$("#fType").html("");
//		$("#fStatus").html("");
		var _idss = row.id;
		$.ajax({
			url : pathUrl("end/loadEndWorkById.action"),
			type : 'POST',
			dataType : "JSON",
			data : {
				"id" : _idss,
			},
			success : function(data) {
				if(data.code == "0000"){
					console.log(data);
				}else{
				}
			},
			error : function(request) {
				swal_Tips_Error();
			}
		});
		
		if(state==1){
			loadData(row,"#checkingForm");
//			$(".lps").html(parseFloat($("#dispatchingfPassQty").val()));//检验数
			$("#dispatchingfPassQty").val(row.fAuxOkQty == null?row.fQty:row.fAuxOkQty);
			initCheckListTable();
			
			$("#checkingModal").modal('show');
			$("#fForitemPercent").val(50.0);
			$("#fCardId").val(row.fCardId);
			$("#fEndWorkId").val(row.id);
		} else if(state==2){
			loadData(row,"#checkForm");
//			console.log(row)
			initChecklistsTable();
			$("#fWorkDate").val(jeDate.timeStampDate(Math.round(new Date()/1000),'YYYY-MM-DD'));
			$("#checkModal").modal('show');
		}
	},
	'click .delete' : function(e, value, row, index) {//删除按钮点击事件
		showSwal(
			'删除',
			'您确定删除这条检查项吗？',
			'是的，确定删除',
			function(result){//isConfirm 点击确定按钮，参数true/false
				if(result.value){
					if(state==1){
						$("#checkListTable").bootstrapTable('removeRow',index);
					} else if(state==2){
//						debugger;
						$("#checklistsTable").bootstrapTable('removeRow',index);
					}
					
				}
			}
		);
	},
	'click .ggxh' : function(e, value, row, index) {//规格型号列点击
//		window.open(pathUrl("DLK?fNumber=")+row.fNumber);//传入参数
		fNumberData = row.fNumber;
		fOperSNData = row.fOperSN;
		initListTable();
		$("#showPDFModal").modal('show');
	},
	'click .TZName' : function(e, value, row, index) {//图纸名称列点击
		window.open(pathUrl("DLK?fName=")+row.fName);//传入参数
//		window.open(pathUrl("DLK?fName=1QJM12-01A3-30-3后盖工艺_1.pdf"));//传入参数
	}
}
/*
 * @Description: 初始化检查项表
 * @param : 
 */
function initCheckListTable(){
	var datas =[];
	console.log($('#yyy').val());
	console.log($('#dispatchingfForitemQty').val());
	console.log($('#dispatchingfOthQty').val());
	var gong = $('#yyy').val();
	var liao = $('#dispatchingfForitemQty').val();
	var ci = $('#dispatchingfOthQty').val();
	console.log(parseInt(gong)+parseInt(liao)+parseInt(ci));
	if(parseInt(gong)+parseInt(liao)+parseInt(ci)==0){
		datas[0]={"fName":"专检","fIsNgObj":0,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":0,"fNote":""};
	}
	else if(parseInt(gong)>0&&parseInt(liao)==0&&parseInt(ci)==0){
		datas[0]={"fName":"专检","fIsNgObj":1,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":gong,"fNote":""};
	}
	else if(parseInt(gong)==0&&parseInt(liao)>0&&parseInt(ci)==0){
		datas[0]={"fName":"专检","fIsNgObj":2,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":liao,"fNote":""};
	}
	else if(parseInt(gong)==0&&parseInt(liao)==0&&parseInt(ci)>0){
		datas[0]={"fName":"专检","fIsNgObj":4,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":ci,"fNote":""};
	}
	else if(parseInt(gong)>0&&parseInt(liao)>0&&parseInt(ci)==0){
		datas[0]={"fName":"专检","fIsNgObj":1,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":gong,"fNote":""};
		datas[1]={"fName":"专检","fIsNgObj":2,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":liao,"fNote":""};
	}
	else if(parseInt(gong)>0&&parseInt(liao)==0&&parseInt(ci)>0){
		datas[0]={"fName":"专检","fIsNgObj":1,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":gong,"fNote":""};
		datas[1]={"fName":"专检","fIsNgObj":4,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":ci,"fNote":""};
	}
	else if(parseInt(gong)==0&&parseInt(liao)>0&&parseInt(ci)>0){
		datas[0]={"fName":"专检","fIsNgObj":2,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":liao,"fNote":""};
		datas[1]={"fName":"专检","fIsNgObj":4,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":ci,"fNote":""};
	}
	else if(parseInt(gong)>0&&parseInt(liao)>0&&parseInt(ci)>0){
		datas[0]={"fName":"专检","fIsNgObj":1,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":gong,"fNote":""};
		datas[1]={"fName":"专检","fIsNgObj":2,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":liao,"fNote":""};
		datas[2]={"fName":"专检","fIsNgObj":4,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":ci,"fNote":""};
	}
	
  /*datas[1]={"fName":"专检","fIsNgObj":2,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":0,"fNote":""};
	datas[2]={"fName":"专检","fIsNgObj":4,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":0,"fNote":""};*/
	//获取模板
	/*$.ajax({
		url : pathUrl("checkTemplateEntry/findByCondition.action"),
		type : "POST",
		dataType : "JSON",
		async : false,
		data : {
			'fNumber' : $("#dispatchingfNumber").val(),//产品代码
			'fOperSN' : $("#dispatchingfOperSN").val()//工序号
		},
		success : function(data) {
//  		console.log(data)
			if(data.message == "没有找到数据"){
				return false;
			}
			$.each(data.rows,function(index,value){
				value.fNGQty = 0;
				if(value.fNote == null){
					value.fNote = "";
				}
				datas[index] = value;
			});
		}
	});*/
	$('#checkListTable').bootstrapTable('destroy').bootstrapTable({
		dataType : 'JSON',//数据类型
		uniqueId : "id",//每一行的唯一标识，一般为主键列
		columns : [{
			field : "fName",
			align : 'left',
			valign: 'middle',
			//sortable : true,
			title : '检查项',
			width : '15%',
			class : 'editfocus'
		},{
			field : "fIsNgObj",
			align : 'left',
			valign: 'middle',
			//sortable : true,
			title : '不良品类型',
			edit:{
				type:'select',//下拉框
				data:[{id:0,text:'无不良品'},{id:1,text:'工废'},{id:2,text:'料废'}/*,{id:3,text:'返修'}*/,{id:4,text:'次品'}],
    			valueField:'id',
    			textField:'text',
    			onSelect:function(val,rec){
//    				console.log(val,rec);
    				var $tr = $(this)[0].$el.closest("tr");
			    	var index = $tr.data('index');
			    	var row = {};
			    	var fNGQty = "";
			    	if(val == 0){
			    		fNGQty = 0;
			    	}
			    	row.fNGQty = fNGQty;
			    	row.fIsNgObj = val;
			    	$('#checkListTable').bootstrapTable('updateRow',{index: index,row:row});
    			}
    		},
			width : '10%'
		},{
			field : "fStandardValue",
			align : 'left',
			title : '标准值',
			width : '6%',
			//sortable : true,
			valign: 'middle'
		},{
			field : "fUpperLimit",
			align : 'left',
			valign: 'middle',
			width : '5%',
			//sortable : true,
			title : '上限'
		},{
			field : "fLowerLimit",
			align : 'left',
			valign: 'middle',
			width : '5%',
			//sortable : true,
			title : '下限'
		}/*,{
			field : "fUnitId",
			align : 'left',
			valign: 'middle',
			//sortable : true,
			title : '单位'
		}*/,{
			field : "fActualValue",
			align : 'left',
			valign: 'middle',
			width : '25%',
			//sortable : true,
			title : '实测值'
		},{
			field : "fQcQty",
			align : 'left',
			valign: 'middle',
			width : '6%',
			//sortable : true,
			title : '检验数',
			formatter: function (value, row, index) {
				row.fQcQty = parseFloat($("#dispatchingfPassQty").val());
				return row.fQcQty;
            }
		},{
			field : "fNGQty",
			align : 'left',
			valign: 'middle',
			width : '6%',
			//sortable : true,
			title : '不良品'
		},{
			field : "fNote",
			align : 'left',
			valign: 'middle',
			width : '15%',
			//sortable : true,
			title : '说明'
		},{
			field : 'operate',//如需导出execl field必填operate，否则操作行会被导出
			align : 'left',
			valign: 'middle',
			width : '5%',
			title : '操作',
			formatter : function(value,row,index){
				if(type == 1){
					return '<button type="button" class="btn btn-danger delete" style="font-size:24px;margin-right:5px;">删除</button>';
				}else{
					return '<button type="button" class="btn btn-danger delete" style="font-size:12px;margin-right:5px;">删除</button>';
				}
			},
			events : operateEvents,
			edit : false
		}],
		data : datas
	});
}
function initChecklistsTable(){
	var datas =[];
	datas[0]={"fName":"专检","fIsNgObj":1,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":0,"fNote":""};
	datas[1]={"fName":"专检","fIsNgObj":2,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":0,"fNote":""};
	datas[2]={"fName":"专检","fIsNgObj":4,"fStandardValue":"","fUpperLimit":"","fLowerLimit":"","fActualValue":"","fQcQty":"","fNGQty":0,"fNote":""};
	/*$.ajax({
		url : pathUrl("checkTemplateEntry/findByCondition.action"),
		type : "POST",
		dataType : "JSON",
		async : false,
		data : {
			'fNumber' : $("#dispatchingfNumber1").val(),//产品代码
			'fOperSN' : $("#dispatchingfOperSN1").val()//工序号
		},
		success : function(data) {
// 		console.log(data)
			if(data.message == "没有找到数据"){
				return false;
			}
			$.each(data.rows,function(index,value){
				value.fNGQty = 0;
				if(value.fNote == null){
					value.fNote = "";
				}
				datas[index] = value;
			});
		}
	});*/
	$('#checklistsTable').bootstrapTable('destroy').bootstrapTable({
		dataType : 'JSON',//数据类型
		uniqueId : "id",//每一行的唯一标识，一般为主键列
		columns : [{
			field : "fName",
			align : 'left',
			valign: 'middle',
			//sortable : true,
			title : '检查项',
			width : '15%',
			class : 'editfocus'
		},{
			field : "fIsNgObj",
			align : 'left',
			valign: 'middle',
			//sortable : true,
			title : '不良品类型',
			edit:{
				type:'select',//下拉框
				data:[{id:0,text:'无不良品'},{id:1,text:'工废'},{id:2,text:'料废'}/*,{id:3,text:'返修'}*/,{id:4,text:'次品'}],
    			valueField:'id',
    			textField:'text',
    			onSelect:function(val,rec){
//    				console.log(val,rec);
    				var $tr = $(this)[0].$el.closest("tr");
			    	var index = $tr.data('index');
			    	var row = {};
			    	var fNGQty = "";
			    	if(val == 0){
			    		fNGQty = 0;
			    	}
			    	row.fNGQty = fNGQty;
			    	row.fIsNgObj = val;
			    	$('#checklistsTable').bootstrapTable('updateRow',{index: index,row:row});
    			}
    		},
			width : '10%'
		},{
			field : "fStandardValue",
			align : 'left',
			title : '标准值',
			width : '6%',
			//sortable : true,
			valign: 'middle'
		},{
			field : "fUpperLimit",
			align : 'left',
			valign: 'middle',
			width : '5%',
			//sortable : true,
			title : '上限'
		},{
			field : "fLowerLimit",
			align : 'left',
			valign: 'middle',
			width : '5%',
			//sortable : true,
			title : '下限'
		}/*,{
			field : "fUnitId",
			align : 'left',
			valign: 'middle',
			//sortable : true,
			title : '单位'
		}*/,{
			field : "fActualValue",
			align : 'left',
			valign: 'middle',
			width : '25%',
			//sortable : true,
			title : '实测值'
		},{
			field : "fQcQty",
			align : 'left',
			valign: 'middle',
			width : '6%',
			//sortable : true,
			title : '检验数',
			formatter: function (value, row, index) {
				row.fQcQty = parseFloat($("#fReceiveQty1").val());
				return row.fQcQty;
            }
		},{
			field : "fNGQty",
			align : 'left',
			valign: 'middle',
			width : '6%',
			//sortable : true,
			title : '不良品'
		},{
			field : "fNote",
			align : 'left',
			valign: 'middle',
			width : '15%',
			//sortable : true,
			title : '说明'
		},{
			field : 'operate',//如需导出execl field必填operate，否则操作行会被导出
			align : 'left',
			valign: 'middle',
			width : '5%',
			title : '操作',
			formatter : function(value,row,index){
				if(type == 1){
					return '<button type="button" class="btn btn-danger delete" style="font-size:24px;margin-right:5px;">删除</button>';
				}else{
					return '<button type="button" class="btn btn-danger delete" style="font-size:12px;margin-right:5px;">删除</button>';
				}
			},
			events : operateEvents,
			edit : false
		}],
		data : datas
	});
}
/*
 * @Description: 新增检查项钮点击事件
 * @param :
 */
$("#addCheck").click(function() {
	$('#checkListTable').bootstrapTable('append',{"fName":"专检","fIsNgObj":0,"fNGQty":0});
});
$("#addChecks").click(function() {
	$('#checklistsTable').bootstrapTable('append',{"fIsNgObj":0,"fNGQty":0});
});
jeDate('#fWorkDate',{
	onClose : false,
	format : 'YYYY-MM-DD',
	donefun : function(obj) {
		// end.minDate = obj.val; // 开始日选好后，重置结束日的最小日期
	}
});
/*
 * @Description: 质检模态框提交按钮单击事件
 * @param :
 */
$("#confirm").click(function() {
	checkList();
});
/*
 * @Description: 外协质检模态框提交按钮单击事件
 * @param :
 */
$("#confirms").click(function() {
	var isEmpty = true;
	var checkListData = $("#checklistsTable").bootstrapTable('getData');
//	console.log(checkListData)
	var length = checkListData.length;//获取检查项列表里有几条数据
	var sum = 0;//各个检验数合计
	var fPassQty = parseFloat($("#dispatchingfPassQty").val());//自检良品
	checkDatas = [];
	if(checkListData.length == 0){
		swal_Tips('请添加检查项！');
		return false;
	}
	$.each(checkListData,function(i,value){
		if(value.fName != "" || i == 0){
			isEmpty = false;
			var fQcQty = value.fQcQty;
			var fNGQty = value.fNGQty;
			var fIsNgObj = value.fIsNgObj;
			if(state==2){
				if($("#fWorkDate").val() == ""){
					swal_Tips('接收日期不能为空！');
					return false;
				}
				if($("#fQualityChkID").val() == ""){
					swal_Tips('检验方式不能为空！');
					return false;
				}
				if($("#fStatu").val() == ""){
					swal_Tips('状态不能为空！');
					return false;
				}
			}
			if(fQcQty === ""){
				swal_Tips('检验数不能为空！');
				return false;
			}
			fQcQty = parseFloat(fQcQty);
			if(isNaN(fQcQty)){
				swal_Tips('请输入正确的检验数！');
				return false;
			}
			if(fQcQty == 0){
				swal_Tips('检验数不能为0！');
				return false;
			}
			if(fQcQty > fPassQty){
				swal_Tips('检验数不能大于自检良品数！');
				return false;
			}
			fNGQty = parseFloat(fNGQty);
			if(fNGQty === ""){
				swal_Tips('不良品不能为空！');
				return false;
			}
			if(isNaN(fNGQty)){
				swal_Tips('请输入正确的不良品！');
				return false;
			}
			if(fNGQty > fQcQty){
				swal_Tips('不良品不能大于检验数！');
				return false;
			}
			if(fIsNgObj == "无不良品"){
				value.fIsNgObj = 0; 
			}
			checkDatas[i] = value;
			sum += fNGQty;
		}
		if(i == length-1){
			if(sum > fPassQty){
				swal_Tips('不良品合计不能大于自检良品数！');
				return false;
			}
			$("#checkModal").modal('hide');
			$("#inputCardModal").modal('show');
		}
	});
	if(isEmpty){
		swal_Tips('请输入检查项目！');
		return false;
	}
});
/*
 * @Description: 检查列表必填项验证方法
 * @param :
 */
function checkList(){
	var isEmpty = true;
	var existForitem = false;
	var checkListData = $("#checkListTable").bootstrapTable('getData');
	var length = checkListData.length;//获取检查项列表里有几条数据
	var sum = 0;//各个检验数合计
	var fPassQty = parseFloat($("#dispatchingfPassQty").val());//自检良品
	checkDatas = [];
	if(checkListData.length == 0){
		swal_Tips('请添加检查项！');
		return false;
	}
	var reg = /^(-?\d+)(\.\d+)?$/;
	if(!reg.test($("#fForitemPercent").val())){
		swal_Tips('请输入正确的料废百分比');
		return false;
	}
	var perValue = parseFloat($("#fForitemPercent").val());
	if(perValue < 0.0 || perValue > 100.0){
		swal_Tips('请输入正确的料废百分比(0-100)');
		return false;
	}
	$.each(checkListData,function(i,value){
		if(value.fIsNgObj == 2){
			existForitem = true;
		}
		
		if(value.fName != "" || i == 0){
			isEmpty = false;
			var fQcQty = value.fQcQty;
			var fNGQty = value.fNGQty;
			var fIsNgObj = value.fIsNgObj;
			if(fQcQty === ""){
				swal_Tips('检验数不能为空！');
				return false;
			}
			fQcQty = parseFloat(fQcQty);
			if(isNaN(fQcQty)){
				swal_Tips('请输入正确的检验数！');
				return false;
			}
			if(fQcQty == 0){
				swal_Tips('检验数不能为0！');
				return false;
			}
			if(fQcQty > fPassQty){
				swal_Tips('检验数不能大于自检良品数！');
				return false;
			}
			fNGQty = parseFloat(fNGQty);
			if(fNGQty === ""){
				swal_Tips('不良品不能为空！');
				return false;
			}
			if(isNaN(fNGQty)){
				swal_Tips('请输入正确的不良品！');
				return false;
			}
			if(fNGQty > fQcQty){
				swal_Tips('不良品不能大于检验数！');
				return false;
			}
			if(fIsNgObj == "无不良品"){
				value.fIsNgObj = 0; 
			}
			checkDatas[i] = value;
			sum += fNGQty;
		}
//		if(i == length - 1){
//			if(existForitem == false && Math.abs($("#fForitemPercent").val()) > Number.EPSILON){
//				swal_Tips('没有料废数，料废百分比应为0');
//				return false;
//			}
//		}
		if(i == length-1){
			if(sum > fPassQty){
				swal_Tips('不良品合计不能大于自检良品数！');
				return false;
			}
			if(type==1){
				$("#checkingModal").modal('hide');
				qualityChecking();
				
			}else{
				$("#checkingModal").modal('hide');
				$("#inputCardModal").modal('show');
			}
		}
	});


	if(isEmpty){
		swal_Tips('请输入检查项目！');
		return false;
	}
}
/*
 * @Description: 质检员卡号模态框显示后操作：质检员卡号获得焦点
 * @param :
 */
$('#inputCardModal').on('shown.bs.modal', function () {
	$("#fWorkernum").focus();
});
/*
 * @Description: 质检员卡号模态框隐藏后，清空质检员卡号
 * @param :
 */
$('#inputCardModal').on('hidden.bs.modal', function () {
	$("#fWorkernum").val("");
});
/*
 * @Description: 质检员卡号模态框輸入完质检员卡号按回车事件
 * @param :
 */
$("#fWorkernum").on("keydown",function(e) {
	if (event.keyCode == "13") {// keyCode=13是回车键
		qualityChecking();
	}
});
/*
 * @Description: 质检员卡号模态框提交按钮单击事件
 * @param :
 */
$("#save").click(function() {
	qualityChecking();
});
/*
 * @Description: 质检提交方法
 * @param :
 */
function qualityChecking(){
	var fWorker = '';
	if(type==0){
		fWorker = $("#fWorkernum").val();
	}else{
		fWorker=$('#dnameid').val();
	}
	var fMacSources;
	console.log($("#checkingForm").serializeObject());
	if(fWorker == ""){
		swal_Tips('请输入质检人卡号！');
		return false;
	}
	showLoading();
	$.ajax({
		url : pathUrl("emp/findByCondition.action"),
		type : 'GET',
		dataType : "JSON",
		data : {
			"row" : 100000,
			"fNumber" : fWorker
		},
		success : function(data) {
			if(data.code == "0000"){
				if(state==1){
					if(type == 0){
						fMacSources = 0;
					}else{
						fMacSources = 1;
					}
					var data=$("#checkingForm").serializeObject()+'&'+$.param({'fMacSource':fMacSources}) + '&' + $.param({'fWorker': fWorker}) + '&' + $.param({'datas': JSON.stringify(checkDatas)});
					console.log(data);
					//showLoading();
					$.ajax({
						url : pathUrl("ipqc/insertSelective.action"),
						type : 'POST',
						dataType : "JSON",
						data : $("#checkingForm").serializeObject() +'&'+$.param({'fMacSource':fMacSources}) + '&' + $.param({'fWorker': fWorker}) + '&' + $.param({'datas': JSON.stringify(checkDatas)}),
						success : function(data) {
//							console.log(data)
							if(data.code == "0000"){
								swal_Tips_Short(data.message);
								quality = true;
								initCheckTableAtCurrentPage(currentPageNumber);
								$("#inputCardModal").modal('hide');
							}else{
								swal_Tips(data.message);
							}
						},
						error : function(request) {
							swal_Tips_Error();
						}
					});
				} else if (state==2) {
//					showLoading();
					var data={};
					//var data=$("#checkForm").serializeObject() +  '&' + $.param({'fWorker': fWorker}) + '&' + $.param({'datas': JSON.stringify(checkDatas)});
					data.fBillNo=$("#fBillNo").val();
					data.fNumber=$("#dispatchingfNumber1").val();
					data.fModel=$("#dispatchingfModel1").val();
					data.fName=$("#dispatchingfName1").val();
					data.fOperSN=$("#dispatchingfOperSN11").val();
					data.fOperName=$("#fOperName1").val();
					data.fCheckQty=$("#fReceiveQty1").val();
					data.fSupplierName=$("#fSupplierName").val();
					data.fWorkDate=new Date($("#fWorkDate").val().replace(/-/g,  "/"));
					data.fQualityChkID=$("#fQualityChkID").val();
					data.fSubcInEntryId=$("#fEntryID").val();
					data.fSubcInId=$("#fInterID").val();
					data.fStatus=$("#fStatu").val();
					data.fWorker=fWorker;
					data.datas=JSON.stringify(checkDatas);
//					console.log(data);
					$.ajax({
						url : pathUrl("iQc/subcInQc.action"),
						type : 'POST',
						dataType : "JSON",
						data : data,
						success : function(data) {
							if(data.code == "0000"){
								swal_Tips_Short(data.message);
								quality = true;
								initCheckTable();
								$("#inputCardModal").modal('hide');
							}else{
								swal_Tips(data.message);
							}
						},
						error : function(request) {
							swal_Tips_Error();
						}
					});
				}
			}else{
				swal_Tips('请输入正确的质检人卡号！');
				$("#fWorker").val("");
//				$("#fWorker").focus();
			}
		},
		error : function(request) {
			swal_Tips_Error();
		}
	});
}
/*
 * @Description: 质检员卡号模态框取消按钮单击事件
 * @param :
 */
$("#back").click(function() {
	if(state==1){
		$("#checkingModal").modal('show');
	} else if(state==2){
		$("#checkModal").modal('show');
	}
});
/*
 * @Description: 清空按钮单击事件
 * @param :
 */
$("#empty").click(function() {
	flag = true;
	reseting();
	$("#condition").focus();
});
/*
 * @Description: 重置
 * @param :
 */
function reseting(){
	$(".clean-C").val("");
	initCheckTable();
	$('.success').removeClass('success');
	rowIndex = "%^%%%%";
	wipeData();
}

/*
 * @Description: 输入操作工姓名/操作工ID/派工单号回车事件
 * @param :
 */
$("#condition").keypress(function(event){
	if(event.keyCode == "13"){
		flag = false;
		initCheckTable();
		wipeData();
	}
});
/*
 * @Description: 输入操作工姓名/操作工ID/派工单号回车事件
 * @param :
 */
function wipeData(){
	if(!quality){
		$('#checkTable').on('load-success.bs.table',function(e,data){
			var name = "";
			var length = data.rows;
			if(length == null && $("#condition").val() != ""){
				swal_Tips("该操作工没有报工信息或该派工单不存在，请重新输入！");
				$(".clean-C").val("");
				flag = true;
				return false;
			}
			if(!flag){
				name = data.rows[0].fWorkerName;
			}
			$("#wname").val(name);
		});
	}
	quality = false;
}
$("#query").click(function(){
	var fWorker = $("#fWorker").val();
	if(fWorker != ""){
		$.ajax({
			url : pathUrl("emp/findByCondition.action"),
			type : 'GET',
			dataType : "JSON",
			data : {
				"row" : 100000,
				"fNumber" : fWorker
			},
			success : function(data) {
//			console.log(data)
				if(data.code == "0000"){
					if(data.rows.length == 1){
						$("#wname").val(data.rows[0].fName);
					}else{
						$("#wname").val("");
					}
				}else{
					$("#wname").val("");
//				$("#fWorker").focus();
				}
			},
			error : function(request) {
				swal_Tips_Error();
			}
		});
	}
	initCheckTable();
});
$("#resetting").click(function(){
	$("#fWorkShopId").val("");
	$("#fBillNo").val("");
	$("#fCode").val("");
	$("#fWorker").val("");
	$("#wname").val("");
	$("#fChecker").val("");
	initCheckTable();
});
/*
 * @Description: 操作工卡号按按退格或delete事件
 * @param :
 */
$("#fWorker").keydown(function(event){
	if(event.keyCode == "8" || event.keyCode == "46"){
		$("#wname").val("");
	}
});
/*
 * @Description: 查询条件按回车事件
 * @param :
 */
$("#fBillNo,#fCode,#fWorker").keypress(function(event){
	if(event.keyCode == "13"){
		if($("#fWorker") != ""){
			$("#query").click();
		}else{
			initCheckTable();
		}
	}
});
/*
 * @Description: more按钮点击事件
 * @param : 
 */
$("#more").click(function(){
	if(flagMore){
		$("#more").text("隐藏");
		$("#extend").show();
		flagMore = false;
	}else{
		$("#more").text("更多");
		$("#extend").hide();
		flagMore = true;
	}
	$('#checkTable').bootstrapTable('resetWidth');
});
